// <editor-fold defaultstate="collapsed" desc="GNU GPLv3 Header">
/*
 * File:   WaveFormatNonPCMByteAlaw.java
 *
 * Copyright (C) 2013 Robert Antoni Buj Gelonch <rbuj@uoc.edu>
 * Copyright (C) 2013 David Megias Jimenez <dmegias@uoc.edu>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
// </editor-fold>
package uoc.libwave;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * Class for storing the ALAW Non-PCM WAVE file format and, for reading and
 * writing its content in Data I/O Streams.
 *
 * @author Robert Antoni Buj Gelonch <rbuj@uoc.edu>
 * @version 1.0
 * @since 2013-08-31
 */
final public class WaveFormatNonPCMByteAlaw extends WaveFormatNonPCMByte {

    /**
     *
     * @param strFilePath
     * @throws FileNotFoundException
     * @throws IOException
     *
     * @since 2013-09-01
     */
    public WaveFormatNonPCMByteAlaw(String strFilePath) throws FileNotFoundException,
            IOException {
        FileInputStream fis = new FileInputStream(strFilePath);
        DataInputStream dis = new DataInputStream(fis);
        WaveFile wavefile = new WaveFile();
        // Header Chunk
        String headerChunkID = wavefile.readCkID(dis);
        if (!headerChunkID.equals("RIFF")) {
            throw new UnsupportedOperationException("headerChunkID != 'RIFF'");
        }
        int headerChunkSize = wavefile.readCkSize(dis);
        String headerRiffType = wavefile.readRiffType(dis);
        if (!headerRiffType.equals("WAVE")) {
            throw new UnsupportedOperationException("headerRiffType != 'WAVE'");
        }
        headerWaveFile = new ChunkHeader(headerChunkID, headerChunkSize,
                headerRiffType);
        // Format Chunk
        String formatChunkID = wavefile.readCkID(dis);
        if (!formatChunkID.equals("fmt ")) {
            throw new UnsupportedOperationException("formatChunkID != 'fmt '");
        }
        int formatChunkSize = wavefile.readCkSize(dis);
        if (formatChunkSize != 18) {
            throw new UnsupportedOperationException("formatChunkSize != 18");
        }
        short wFormatTag = wavefile.readWFormatTag(dis);
        if (wFormatTag != WAVE_FORMAT_ALAW) {
            throw new UnsupportedOperationException("wFormatTag != WAVE_FORMAT_ALAW");
        }
        short wChannels = wavefile.readWChannels(dis);
        int dwSamplesPerSec = wavefile.readDWSamplesPerSec(dis);
        int dwAvgBytesPerSec = wavefile.readDWAvgBytesPerSec(dis);
        short wBlockAlign = wavefile.readWBlockAlign(dis);
        short wBitsPerSample = wavefile.readWBitsPerSample(dis);
        if (wBitsPerSample != 8) {
            throw new UnsupportedOperationException("wBitsPerSample != 8");
        }
        short cbSize = wavefile.readCbSize(dis);
        if (cbSize != 0) {
            throw new UnsupportedOperationException("cbSize != 0");
        }
        chunkFormatNonPCM = new ChunkFormatNonPCM(formatChunkID, formatChunkSize,
                wFormatTag, wChannels, dwSamplesPerSec, dwAvgBytesPerSec,
                wBlockAlign, wBitsPerSample, cbSize);
        // Fact Chunk
        String factChunkID = wavefile.readCkID(dis);
        if (!factChunkID.equals("fact")) {
            throw new UnsupportedOperationException("formatChunkID != 'data'");
        }
        int factChunkSize = wavefile.readCkSize(dis);
        if (factChunkSize != 4) {
            throw new UnsupportedOperationException("factChunkSize != 4");
        }
        int dwSampleLength = wavefile.readDwSampleLength(dis);
        chunkFact = new ChunkFact(factChunkID, factChunkSize, dwSampleLength);
        // Data Chunk
        String dataChunkID = wavefile.readCkID(dis);
        if (!dataChunkID.equals("data")) {
            throw new UnsupportedOperationException("formatChunkID != 'data'");
        }
        int dataChunkSize = wavefile.readCkSize(dis);
        chunkData = new ChunkDataNonPCMByte(dataChunkID, dataChunkSize,
                wBitsPerSample, wChannels, dis);
        // Close I/O streams
        dis.close();
        fis.close();
    }

    /**
     *
     * @param sample_data
     * @param lenght
     * @param wChannel
     * @param dwSamplesPerSec
     *
     * @since 2013-08-31
     */
    public WaveFormatNonPCMByteAlaw(byte[][] sample_data, int lenght,
            short wChannel, int dwSamplesPerSec) {
        super(WAVE_FORMAT_ALAW, sample_data, lenght, wChannel, dwSamplesPerSec);
    }
}
